package com.cloud.utils.common;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;



/**
 * jwt加密解密类
 * @author Owner
 *
 */
public class JwtUtil {

    //秘钥
    private static final String keyt="79e7c69681b8270162386e6daa53d1dc";




    /**
     * map解密
     * @param jsonWebToken
     * @param jsonWebToken
     * @return
     */
    public static Map<String, Object> parseJWTMap(String jsonWebToken) throws Exception {
        Claims claims=null;
        try {
            claims = Jwts.parser()
                    .setSigningKey(keyt.getBytes())
                    .parseClaimsJws(jsonWebToken).getBody();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        if(claims == null ) {
            return null;
        }



        return claims;



    }


    /**
     * map加密
     * @param map
     * @return
     */
    public static String createJWT(Map<String, Object> map) throws Exception{
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        //添加构成JWT的参数
        JwtBuilder builder = Jwts.builder().setHeaderParam("typ", "JWT")
                .setPayload(JSON.toJSONString(map))
                .signWith(signatureAlgorithm,keyt.getBytes()); //估计是第三段密钥
        //生成JWT
        return builder.compact();
    }





    /**
     * Object加密
     * @param object
     * @return
     */
    public static String createJWT(Object  object) throws Exception{
        Map<String, Object> userMap=new HashMap<String, Object> ();
        userMap.put("token", object);
        //添加构成JWT的参数
        String token = Jwts.builder()
                .setHeader(userMap)

                .setExpiration(new Date(System.currentTimeMillis() + 60 * 60 * 24 * 1000))
                .signWith(SignatureAlgorithm.HS512,  keyt.getBytes())
                .compact();
        //生成JWT
        return token;
    }


    /**
     * Object串解密
     * @param token
     * @return
     */
    public  static Object parseJWTObject(String token)throws Exception {
        Object author = Jwts.parser()
                .setSigningKey(keyt.getBytes())
                .parseClaimsJws(token)
                .getHeader().get("token");


        return author;
    }


    /**
     *单字符串 加密
     * @param str
     * @return
     */
    public static String createJWT(String str) throws Exception{
        //添加构成JWT的参数
        String token = Jwts.builder()
                .setSubject(str)
                .signWith(SignatureAlgorithm.HS512, keyt.getBytes())
                .compact();

        return token;
    }



    /**
     * 单字符串解密
     * @param token
     * @return
     */
    public  static String parseJWT(String token)throws Exception {
        String author = Jwts.parser()
                .setSigningKey(keyt.getBytes())
                .parseClaimsJws(token)
                .getBody()
                .getSubject();
        return author;
    }



    public static void main(String[] args) throws Exception{

        Map<String, Object>  map = new HashMap<>();
        map.put("token_user_name", "admin");
        map.put("time", System.currentTimeMillis());

        String token =  createJWT(map);
        System.err.println("====>加密的Token"+token);

        String name = (String) parseJWTMap(token).get("token_user_name");
        System.err.println("====>解密的用户名称："+name);
        System.err.println(JwtUtil.class+".main(..)");
    }


}
